/*
JSNES, based on Jamie Sanders' vNES
Copyright (C) 2010 Ben Firshman

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

// Keyboard events are bound in the UI
JSNES.Keyboard = function () {
  var i;

  this.keys = {
    KEY_A: 0,
    KEY_B: 1,
    KEY_SELECT: 2,
    KEY_START: 3,
    KEY_UP: 4,
    KEY_DOWN: 5,
    KEY_LEFT: 6,
    KEY_RIGHT: 7,
  };

  this.state1 = new Array(8);
  for (i = 0; i < this.state1.length; i++) {
    this.state1[i] = 0x40;
  }
  this.state2 = new Array(8);
  for (i = 0; i < this.state2.length; i++) {
    this.state2[i] = 0x40;
  }
};

JSNES.Keyboard.prototype = {
  setKey: function (key, value) {
    switch (key) {
      case 75:
        this.state1[this.keys.KEY_A] = value;
        break; // X 88
      case 74:
        this.state1[this.keys.KEY_B] = value;
        break; // Y (Central European keyboard) 89
      case 17:
        this.state1[this.keys.KEY_SELECT] = value;
        break; // Right Ctrl
      case 13:
        this.state1[this.keys.KEY_START] = value;
        break; // Enter
      case 87:
        this.state1[this.keys.KEY_UP] = value;
        break; // Up 38
      case 83:
        this.state1[this.keys.KEY_DOWN] = value;
        break; // Down 40
      case 65:
        this.state1[this.keys.KEY_LEFT] = value;
        break; // Left 37
      case 68:
        this.state1[this.keys.KEY_RIGHT] = value;
        break; // Right 39

      case 100:
        this.state2[this.keys.KEY_A] = value;
        break; // Num-7
      case 101:
        this.state2[this.keys.KEY_B] = value;
        break; // Num-9
      case 99:
        this.state2[this.keys.KEY_SELECT] = value;
        break; // Num-3
      case 97:
        this.state2[this.keys.KEY_START] = value;
        break; // Num-1
      // case 104: this.state2[this.keys.KEY_UP] = value; break;    // Num-8
      // case 98: this.state2[this.keys.KEY_DOWN] = value; break;   // Num-2
      // case 100: this.state2[this.keys.KEY_LEFT] = value; break;  // Num-4
      // case 102: this.state2[this.keys.KEY_RIGHT] = value; break; // Num-6
      case 38:
        this.state2[this.keys.KEY_UP] = value;
        break; // ↑
      case 40:
        this.state2[this.keys.KEY_DOWN] = value;
        break; // ↓
      case 37:
        this.state2[this.keys.KEY_LEFT] = value;
        break; // ←
      case 39:
        this.state2[this.keys.KEY_RIGHT] = value;
        break; // →
      default:
        return true;
    }
    return false; // preventDefault
  },

  //   keyDown: function (evt) {
  //     if (!this.setKey(evt.keyCode, 0x41) && evt.preventDefault) {
  //       evt.preventDefault();
  //     }
  //   },

  //   keyUp: function (evt) {
  //     if (!this.setKey(evt.keyCode, 0x40) && evt.preventDefault) {
  //       evt.preventDefault();
  //     }
  //   },

  keyDown: function (evt) {
    var that = this;
    //console.log("sekey " + evt.keyCode);
    if (evt.keyCode == 85 || evt.keyCode == 73) {
      // 按键 U I，连发和连跳
      var keyno;
      if (evt.keyCode == 85) keyno = this.keys.KEY_B;
      else if (evt.keyCode == 73) keyno = this.keys.KEY_A;
      that.state1[keyno] = that.state1[keyno] != 0x41 ? 0x41 : 0x40;
      setTimeout(function () {
        that.state1[keyno] = that.state1[keyno] != 0x41 ? 0x41 : 0x40;
      }, 50);

      if (evt.preventDefault) {
        evt.preventDefault();
      }
      return;
    }
    if (evt.keyCode == 103 || evt.keyCode == 104) {
        // 按键 U I，连发和连跳
        var keyno;
        if (evt.keyCode == 103) keyno = this.keys.KEY_B;
        else if (evt.keyCode == 104) keyno = this.keys.KEY_A;
        that.state2[keyno] = that.state2[keyno] != 0x41 ? 0x41 : 0x40;
        setTimeout(function () {
          that.state2[keyno] = that.state2[keyno] != 0x41 ? 0x41 : 0x40;
        }, 50);
  
        if (evt.preventDefault) {
          evt.preventDefault();
        }
        return;
      }
    if (!this.setKey(evt.keyCode, 0x41) && evt.preventDefault) {
      evt.preventDefault();
    }
  },

  keyUp: function (evt) {
    if (evt.keyCode == 85 || evt.keyCode == 73) {
      // 按键 U I，连发和连跳
      if (evt.keyCode == 85) this.setKey(74, 0x40); // KEY_B-J-74
      else if (evt.keyCode == 73) this.setKey(75, 0x40); // KEY_A-K-75
      if (evt.preventDefault) {
        evt.preventDefault();
      }
      return;
    }
    if (!this.setKey(evt.keyCode, 0x40) && evt.preventDefault) {
      evt.preventDefault();
    }
  },

  keyPress: function (evt) {
    evt.preventDefault();
  },
};
